package org.jeecg.modules.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.vehicle.entity.WlhyVehicle;
import org.jeecg.modules.vehicle.mapper.WlhyVehicleMapper;
import org.jeecg.modules.vehicle.req.WlhyVehicleBody;
import org.jeecg.modules.vehicle.service.IWlhyVehicleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import static org.jeecg.common.constant.WlhyBizConstant.*;
import static org.jeecg.common.constant.WlhyBizConstant.ROW_STATUS3;

/**
 * @Description: 车辆表
 * @Author: jeecg-boot
 * @Date:   2024-04-15
 * @Version: V1.0
 */
@Service
public class WlhyVehicleServiceImpl extends ServiceImpl<WlhyVehicleMapper, WlhyVehicle> implements IWlhyVehicleService {

    @Override
    public WlhyVehicle getByUserId(String userId) {
        return  getOne(new LambdaQueryWrapper<WlhyVehicle>().eq(WlhyVehicle::getUserId,userId).eq(WlhyVehicle::getStatus,ROW_STATUS1));
    }

    @Override
    public void add(WlhyVehicleBody body) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        WlhyVehicle wlhyVehicle = getByUserId(loginUser.getId());
        if (null != wlhyVehicle) {// 编辑

            if (!wlhyVehicle.getUserId().equals(loginUser.getId())) {
                throw new RuntimeException("非法操作");
            }
            BeanUtils.copyProperties(body,wlhyVehicle);
            wlhyVehicle.setUserId(loginUser.getId());
            wlhyVehicle.setUserPhone(loginUser.getPhone());

            wlhyVehicle.setStatus(ROW_STATUS1);
            wlhyVehicle.setVehicleStatus(VEHICLE_STATUS1);
            updateById(wlhyVehicle);
        }else{
            wlhyVehicle   = new WlhyVehicle();
            BeanUtils.copyProperties(body,wlhyVehicle);
            wlhyVehicle.setUserId(loginUser.getId());
            wlhyVehicle.setUserPhone(loginUser.getPhone());
            wlhyVehicle.setStatus(ROW_STATUS1);
            wlhyVehicle.setVehicleStatus(VEHICLE_STATUS1);
            save(wlhyVehicle);
        }

    }

    @Override
    public void deleteById(String id) {
        update(new LambdaUpdateWrapper<WlhyVehicle>().eq(WlhyVehicle::getId, id).set(WlhyVehicle::getStatus, ROW_STATUS0));
    }

    @Override
    public void deleteBatchByIds(List<String> ids) {
        update(new LambdaUpdateWrapper<WlhyVehicle>().in(WlhyVehicle::getId, ids).set(WlhyVehicle::getStatus, ROW_STATUS0));

    }
}
